package com.yupi.yuoj.model.vo;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yupi.yuoj.model.entity.MembershipPlan;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import lombok.Data;
import org.springframework.beans.BeanUtils;

/**
 * 会员套餐视图
 *
 * @author jingjin
 */
@Data
public class MembershipPlanVO implements Serializable {

    private final static Gson GSON = new Gson();

    /**
     * id
     */
    private Long id;

    /**
     * 套餐名称
     */
    private String name;

    /**
     * 套餐描述
     */
    private String description;

    /**
     * 套餐类型（monthly-月度, yearly-年度, lifetime-终身）
     */
    private String type;

    /**
     * 套餐时长（天）
     */
    private Integer duration;

    /**
     * 价格
     */
    private BigDecimal price;

    /**
     * 原价
     */
    private BigDecimal originalPrice;

    /**
     * 功能特性列表
     */
    private List<String> featureList;

    /**
     * 是否推荐
     */
    private Integer isRecommended;

    /**
     * 排序
     */
    private Integer sortOrder;

    /**
     * 状态（0-禁用, 1-启用）
     */
    private Integer status;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 折扣百分比
     */
    private Integer discountPercentage;

    /**
     * 包装类转对象
     *
     * @param membershipPlanVO
     * @return
     */
    public static MembershipPlan voToObj(MembershipPlanVO membershipPlanVO) {
        if (membershipPlanVO == null) {
            return null;
        }
        MembershipPlan membershipPlan = new MembershipPlan();
        BeanUtils.copyProperties(membershipPlanVO, membershipPlan);
        List<String> featureList = membershipPlanVO.getFeatureList();
        if (featureList != null) {
            membershipPlan.setFeatures(GSON.toJson(featureList));
        }
        return membershipPlan;
    }

    /**
     * 对象转包装类
     *
     * @param membershipPlan
     * @return
     */
    public static MembershipPlanVO objToVo(MembershipPlan membershipPlan) {
        if (membershipPlan == null) {
            return null;
        }
        MembershipPlanVO membershipPlanVO = new MembershipPlanVO();
        BeanUtils.copyProperties(membershipPlan, membershipPlanVO);
        String featuresJson = membershipPlan.getFeatures() != null ? membershipPlan.getFeatures().toString() : null;
        List<String> featureList = null;
        if (featuresJson != null && !featuresJson.isEmpty()) {
            featureList = GSON.fromJson(featuresJson, new TypeToken<List<String>>() {
            }.getType());
        }
        membershipPlanVO.setFeatureList(featureList);
        
        // 计算折扣百分比
        if (membershipPlan.getOriginalPrice() != null && membershipPlan.getPrice() != null 
            && membershipPlan.getOriginalPrice().compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal discount = membershipPlan.getOriginalPrice().subtract(membershipPlan.getPrice());
            BigDecimal discountPercentage = discount.divide(membershipPlan.getOriginalPrice(), 2, BigDecimal.ROUND_HALF_UP)
                    .multiply(new BigDecimal(100));
            membershipPlanVO.setDiscountPercentage(discountPercentage.intValue());
        }
        
        return membershipPlanVO;
    }

    private static final long serialVersionUID = 1L;
}